//
//  main.m
//  05-集合的循环遍历和数组排序
//
//  Created by dllo on 16/7/8.
//  Copyright © 2016年 daming. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[]) {
#if 0
    // 1.定义一个数组, 包含五个字符串对象, 倒序取出数组中的所有元素, 并存储到一个可变数组中
    NSArray *arr = @[@"我是", @"不是", @"你", @"天天", @"你猜"];
    // 2.创建可变数组
    NSMutableArray *muArr = [NSMutableArray array];
    // 3.倒序循环遍历
    for (NSInteger i = arr.count - 1; i >= 0; i--) {
        [muArr addObject:arr[i]];
    }
    NSLog(@"%@", muArr);
#endif
    // 定义一个字典, 用于存储一个人的name, sex, age三个数据, 完成对其三个数据的输出
#if 0
    NSDictionary *dic = @{@"name": @"张三",
                          @"sex" : @"male",
                          @"age" : @20};
    
    // 取出所有的key
    NSArray *keys = [dic allKeys];
    
    NSMutableArray *values = [NSMutableArray array];
    for (int i = 0; i < keys.count; i++) {
        //创建字符串key
        NSString *key = keys[i];
        //创建id类型value
        id value = [dic objectForKey:key];
        NSLog(@"%@" , value);
        [values addObject:[dic objectForKey:keys[i]]];
    }
    NSLog(@"%@", values);
#endif
#if 0
    // 枚举器
    // 数组的枚举器
    // NSEnumerator依赖于集合类型(数组, 字典, 集合), 不能单独使用]
    NSArray *arr = @[@"我是", @"不是", @"你", @"天天", @"你猜"];
    NSEnumerator *stringArrayEnumerator = [arr objectEnumerator];
    id value = nil;
    while (value = [stringArrayEnumerator nextObject]) {
        NSLog(@"%@", value);
    }
    // 倒序枚举
    NSEnumerator *reverse = [arr reverseObjectEnumerator];
    id value1 = nil;
    while (value1 = [reverse nextObject]) {
        NSLog(@"%@", value1);
    }
#endif
#if 0
    // 字典的枚器(字典无序, 所以不能倒序)
    // 默认取出来的值, 是value
    NSDictionary *dic = @{@"name": @"张三",
                          @"sex" : @"male",
                          @"age" : @20};
    NSEnumerator *dicEnumerator = [dic objectEnumerator];
    id dicValue = nil;
    while (dicValue = [dicEnumerator nextObject]) {
        NSArray *keys = [dic allKeysForObject:dicValue];
        NSLog(@"keys = %@, value = %@", keys, dicValue);
    }
#endif
#if 0
    // 集合枚举
    NSSet *set = [NSSet setWithObjects:@"110", @"120", @"234", @"546", nil];
    NSEnumerator *setEnumerator = [set objectEnumerator];
    id setValue = nil;
    while (setValue = [setEnumerator nextObject]) {
        NSLog(@"%@", setValue);
    }
#endif
#if 0
    // 快速枚举 for...in
    NSArray *arr = @[@"我是", @"不是", @"你", @"天天", @"你猜"];
    NSString *str2 = [[NSString alloc]init];
    for (NSString *str in arr) {
        str2 = [str2 stringByAppendingString:str];
    }
        NSLog(@"%@", str2);
    // 快速枚举一个字典
    NSDictionary *dic = @{@"name": @"张三",
                          @"sex" : @"male",
                          @"age" : @20};
    for (NSString *key in dic) {
        NSString *value = [dic valueForKey:key];
        NSLog(@"%@", value);
    }
    //快速枚举一个集合
    NSSet *set = [NSSet setWithObjects:@"110", @"120", @"234", @"546", nil];
    for (NSString *string in set) {
        NSLog(@"%@", string);
    }
#endif
#if 0
    // 排序
    // 参数1 key: 排序条件
    // 参数2 acending: YES(升序) NO(降序)
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:YES];
    NSArray *arr = @[@10, @3, @2, @5, @4];
    NSMutableArray *arr1 = [NSMutableArray arrayWithObjects:@4, @5,@6, @2, @1, nil];
    arr = [arr sortedArrayUsingDescriptors:@[sortDescriptor]];
    [arr1 sortUsingDescriptors:@[sortDescriptor]];
    NSLog(@"%@", arr);
    NSLog(@"%@", arr1);
    // 排序: 可变数组和不可变数组通过sort排序调用的方法不同
    // 1.不可变数组, 排序后需要生成新的数组
    // 2.可变数组, 排序直接可以在本身之上修改
#endif
#if 1
    Person *per1 = [[Person alloc]initWithName:@"张三" age:20];
    Person *per2 = [[Person alloc]initWithName:@"张三" age:24];
    Person *per3 = [[Person alloc]initWithName:@"张三" age:18];
    NSArray *perArray = @[per1, per2, per3];
    NSLog(@"排序前:%@", perArray);
    // 根据属性age排序
    NSSortDescriptor *perSort = [[NSSortDescriptor alloc] initWithKey:@"age" ascending:YES];
    perArray = [perArray sortedArrayUsingDescriptors:@[perSort]];
    NSLog(@"排序后:%@", perArray);
    
    // 排序2
    NSArray *sortString = @[@"iPhone", @"iPad", @"iTouch", @"iMac", @"Mac Mini"];
    sortString = [sortString sortedArrayUsingSelector:@selector(compare:)];
    NSLog(@"%@", sortString);
#endif
    
    return 0;
}
